{include file="public/nheader" /}
<div class="layui-fluid layui-bg-white">
    <form action="{:url()}" class="layui-form layui-form-fixed">
        <div class="layui-form-item">
            <label class="layui-form-label">
                <font color="red">* </font>活动名称
            </label>
            <div class="layui-input-block">
                <input type="text" id="title" name="title" required="" lay-verify="required" autocomplete="off"
                    class="layui-input" value="{$row.title|default=''}" placeholder="">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">持码代理[添加后不可更改]
            </label>
            <div class="layui-input-block">
                <select name="agentid" lay-search='' lay-filter="agentid">
                    <option {if !empty($row.id) && $row.id> 0}disabled{/if} value="">请选择代理</option>
                    <option {if !empty($row.id) && $row.id> 0}disabled{/if} value="0" {if empty($row.agentid) &&
                        $row.agentid==0}selected{/if}>平台</option>
                    {foreach $agent as $v}
                    <option {if !empty($row.id) && $row.id> 0}disabled{/if} value="{$v.id}" {if !empty($row.agentid) &&
                        $row.agentid==$v.id}selected{/if}>
                        {$v.nickname}({$v.accounts})</option>
                    {/foreach}
                </select>
            </div>
        </div>
        <!-- <div class="layui-form-item">
            <label class="layui-form-label">
                <font color="red">* </font>秘钥编码前缀
            </label>
            <div class="layui-input-block">
                <input type="text" id="code" name="code" required="" lay-verify="required" autocomplete="off"
                    class="layui-input" value="{$row.code|default=''}" placeholder="如深圳地区可以填写,最多不超过4位数:SZ">
            </div>
        </div> -->
        <div class="layui-form-item">
            <label class="layui-form-label">
                <font color="red">* </font>购买价格
            </label>
            <div class="layui-input-block">
                <input type="number" id="money" name="money" required="" lay-verify="required" autocomplete="off"
                    class="layui-input" value="{$row.money|default=''}" placeholder="">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
                <font color="red">* </font>兑换码数量
            </label>
            <div class="layui-input-block" style="color:red ;">
                请创建完活动后手动生成
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
                <font color="red">* </font>活动状态
            </label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" title="开启" {if empty($row.status) ||
                    $row.status==1}checked{/if}>
                <input type="radio" name="status" value="2" title="结束" {if !empty($row.status) &&
                    $row.status==2}checked{/if}>
            </div>
        </div>
        <div class="layui-card layui-form">
            <label class="layui-form-label">
                参数值(补贴金额表示单券补贴给用券店面的金额)
            </label>
            <div class="layui-card-body">
                <div class="layui-input-block">
                    <a class="layui-btn layui-btn-btn layui-btn-sm layui-btn-normal" onclick="addtable2()">添加券</a>
                    <table class="layui-table layui-form" style="border:1px solid #ccc;" id="table2"
                        lay-filter="table2">
                        <thead>
                            <tr>
                                <td>
                                    <div class="layui-table-cell">序号</div>
                                </td>
                                <td>
                                    <div class="layui-table-cell">有效项目类型</div>
                                </td>
                                <td>
                                    <div class="layui-table-cell">有效车类型</div>
                                </td>
                                <td>
                                    <div class="layui-table-cell">有效期天数</div>
                                </td>
                                <td>
                                    <div class="layui-table-cell">发放数量</div>
                                </td>
                                <td>
                                    <div class="layui-table-cell">操作</div>
                                </td>
                            </tr>
                        </thead>
                        <tbody>
                            {if !empty($row.coupon)}
                            {foreach $row.coupon as $k => $v}
                            <tr style="width:100px">
                                <td>{$k+1}</td>
                                <td>
                                    {foreach :config()['config']['type'] as $s => $vs}
                                    {if $s > 0}
                                    <input name="coupon[{$k}][type][]" {if in_array($s, $v.type)} checked {/if}
                                        lay-skin="primary" type="checkbox" value="{$s}" title="{$vs}"><br />
                                    {/if}
                                    {/foreach}
                                </td>
                                <td>
                                    {foreach :config()['config']['carmodel'] as $s => $vs}
                                    {if $s > 0}
                                    <input name="coupon[{$k}][carmodel][]" {if in_array($s, $v.carmodel)} checked {/if}
                                        lay-skin="primary" type="checkbox" value="{$s}" title="{$vs}"><br />
                                    {/if}
                                    {/foreach}
                                </td>
                                <td><input required="" lay-verify="required" type="text" id="layui-input"
                                        class="layui-input" name="coupon[{$k}][days]" value="{$v.days|default=0}"></td>
                                <td><input required="" lay-verify="required" type="text" id="layui-input"
                                        class="layui-input" name="coupon[{$k}][number]" value="{$v.number|default=0}">
                                </td>
                                <td><a class="layui-btn layui-btn-danger layui-btn-xs del2">删除</a></td>
                            </tr>
                            {/foreach}
                            {/if}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">
                <font color="red">* </font>活动说明
            </label>
            <div class="layui-input-block">
                <textarea name="contents" class="layui-textarea" >{$row.contents|default=''}</textarea>
            </div>
        </div>
        <div class="layui-footer layui-form-item layui-center ">
            <input type="hidden" value="{$row['id']|default='0'}" name="id" />
            <button class="layui-btn" lay-filter="submitIframe" type="button" lay-submit>提交</button>
            <button class="layui-btn layui-btn-primary" type="button" sa-event="closeDialog">取消</button>
        </div>
    </form>
</div>
{include file="public/nfooter" /}
<script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<script>
    layui.use(['form', 'jquery', 'layedit', 'admin', 'content'], function () {
        var form = layui.form;
        var admin = layui.admin;
        var jquery = layui.jquery;
        // 渲染富文本编辑器
        var layedit = layui.layedit;
        var content = layui.content;
        content.tinymce('contents');
    });
    function addtable2() {
        var i = $('#table2 tbody tr').length;
        var json = {
            "1": "外观清洗",
            "2": "内外清洗",
            "3": "精致清洗",
            "4": "除虫除胶",
            "5": "沥青处理",
            "6": "皮革保养护理",
            "7": "内饰镀膜",
            "99": "其它服务",
        };
        var json2 = {
            "1": "轿车",
            "2": "SUV",
            "3": "七座",
        };
        var check = '';
        $.each(json, function (m, obj) {
            if (m <= 1) {
                check += '<input name="coupon[' + i + '][type][]" lay-skin="primary" checked type="checkbox" value="' + m + '" title="' + obj + '" ><br/>'
            } else {
                check += '<input name="coupon[' + i + '][type][]" lay-skin="primary" type="checkbox" value="' + m + '" title="' + obj + '" ><br/>'
            }
        });
        var check2 = '';
        $.each(json2, function (m, obj) {
            check2 += '<input name="coupon[' + i + '][carmodel][]" lay-skin="primary" checked type="checkbox" value="' + m + '" title="' + obj + '" ><br/>'
        });
        //你要添加的html
        var html = '<tr ><td>' + (i + 1) + '</td><td>' + check + '</td>' +
            '<td>' + check2 + '</td>' +
            '<td><input required="" lay-verify="required" type="text" id="layui-input" class="layui-input" name="coupon[' + i + '][days]" value="365"></td>' +
            '<td><input required="" lay-verify="required" type="text" id="layui-input" class="layui-input" name="coupon[' + i + '][number]" value="1"></td>' +
            '<td><a class="layui-btn layui-btn-danger layui-btn-xs del2" >删除</a></td></tr>';
        $(html).appendTo($('#table2 tbody:last'));
        layui.use(['form'], function () {
            $ = layui.jquery;
            var form = layui.form;
            form.render();
        });
    };
    $('body').on('click', '.del2', function () {
        $(this).closest('tr').remove();
    });
</script>